package com.cgy.wodeyigui.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.cgy.wodeyigui.product.entity.AttrEntity;
import com.cgy.wodeyigui.product.service.AttrService;
import com.cgy.wodeyigui.product.service.CategoryService;
import com.cgy.wodeyigui.product.vo.AttrGroupRelationVO;
import com.cgy.wodeyigui.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cgy.wodeyigui.product.entity.AttrGroupEntity;
import com.cgy.wodeyigui.product.service.AttrGroupService;
import com.cgy.common.utils.PageUtils;
import com.cgy.common.utils.R;

import javax.websocket.server.PathParam;


/**
 * 属性分组
 *
 * @author Chen
 * @email 481691470@qq.com
 * @date 2022-01-12 10:51:50
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    AttrService attrService;
    ///product/attrgroup/{attrgroupId}/attr/relation
    //获取属性分组的关联的所有属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities= attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }
//product/attrgroup/{catelogId}/withattr
//获取分类下所有分组&关联属性
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs (@PathParam("catelogId")Long catelogId){
        //1:查询当前类下的所有属性分组
        //2:查出每个属性分组的所有属性
       List<AttrGroupWithAttrsVo> Vos= attrGroupService.getAttrGroupWithAttrsBycatelogId(catelogId);
       return R.ok().put("data",Vos);
    }




    //删除属性于分组的关联关系  /product/attrgroup/attr/relation/delete

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
       PageUtils page= attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
         Long[] path =  categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){

		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
